class Solution {
public:
    int superpalindromesInRange(string left, string right) {
        int res = 0;
        long l = stol(left), r = stol(right);
        //处理偶数
        for (int i = 1; i < 100000; i++) {
            string s = to_string(i);
            string s2 = s;
            reverse(s2.begin(), s2.end());
            long m = stol(s + s2);
            m *= m;
            if (m > r) break;
            if (m >= l && is_reverse(m)) {
                res++;
            }
        }
        //处理奇数
        for (int i = 1; i < 100000; ++i) {
            string s = to_string(i);
            string s2 = s;
            reverse(s2.begin(), s2.end());
            long m = stol(s.append(s2.begin() + 1, s2.end()));
            m *= m;
            if (m > r) break;
            if (m >= l && is_reverse(m)) {
                ++res;
            }
        }
        return res;
    }
    bool is_reverse(long num) {
        string s = to_string(num);
        int i = 0, j = s.size() - 1;
        while (i < j) {
            if (s[i] != s[j]) return false;
            i++, j--;
        }
        return true;
    }
};
